|
1
|
|
|
/* |
|
2
|
|
|
* read-bytes: Function to read data from bytes. |
|
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
|
4
|
|
|
* https://github.com/rochars/byte-data |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
let helpers = require("../src/helpers.js"); |
|
8
|
|
|
const floats = require("../src/floats.js"); |
|
9
|
|
|
const intBits = require("int-bits"); |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Read a group of bytes by turning it to bits. |
|
13
|
|
|
* Useful for 40 & 48-bit, but underperform. |
|
14
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
15
|
|
|
* @param {number} i The index to read. |
|
16
|
|
|
* @param {number} numBytes The number of bytes |
|
17
|
|
|
* (1 for 8-bit, 2 for 16-bit, etc). |
|
18
|
|
|
* @return {number} |
|
19
|
|
|
*/ |
|
20
|
|
|
function readBytesAsBits(bytes, i, numBytes) { |
|
21
|
|
|
let j = numBytes-1; |
|
22
|
|
|
let bits = ""; |
|
23
|
|
|
while (j >= 0) { |
|
24
|
|
|
bits += helpers.bytePadding(bytes[j + i].toString(2), 2); |
|
25
|
|
|
j--; |
|
26
|
|
|
} |
|
27
|
|
|
return parseInt(bits, 2); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
const BitReader = { |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Read 1 8-bit int from from bytes. |
|
34
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
35
|
|
|
* @param {number} i The index to read. |
|
36
|
|
|
* @return {number} |
|
37
|
|
|
*/ |
|
38
|
|
|
"read8Bit": function (bytes, i) { |
|
39
|
|
|
return bytes[i]; |
|
40
|
|
|
}, |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Read 1 16-bit int from from bytes. |
|
44
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
45
|
|
|
* @param {number} i The index to read. |
|
46
|
|
|
* @return {number} |
|
47
|
|
|
*/ |
|
48
|
|
|
"read16Bit": function (bytes, i) { |
|
49
|
|
|
return bytes[1 + i] << 8 | bytes[i]; |
|
50
|
|
|
}, |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Read 1 16-bit float from from bytes. |
|
54
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
55
|
|
|
* @param {number} i The index to read. |
|
56
|
|
|
* @return {number} |
|
57
|
|
|
*/ |
|
58
|
|
|
"read16BitFloat": function (bytes, i) { |
|
59
|
|
|
return floats.decodeFloat16(bytes.slice(i,i+2)); |
|
60
|
|
|
}, |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Read 1 24-bit int from from bytes. |
|
64
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
65
|
|
|
* @param {number} i The index to read. |
|
66
|
|
|
* @return {number} |
|
67
|
|
|
*/ |
|
68
|
|
|
"read24Bit": function (bytes, i) { |
|
69
|
|
|
return bytes[2 + i] << 16 | |
|
70
|
|
|
bytes[1 + i] << 8 | |
|
71
|
|
|
bytes[i]; |
|
72
|
|
|
}, |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Read 1 32-bit int from from bytes. |
|
76
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
77
|
|
|
* @param {number} i The index to read. |
|
78
|
|
|
* @return {number} |
|
79
|
|
|
*/ |
|
80
|
|
|
"read32Bit": function (bytes, i) { |
|
81
|
|
|
return (bytes[3 + i] << 24 | |
|
82
|
|
|
bytes[2 + i] << 16 | |
|
83
|
|
|
bytes[1 + i] << 8 | |
|
84
|
|
|
bytes[i]) >>> 0; |
|
85
|
|
|
}, |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Read 1 32-bit float from from bytes. |
|
89
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
90
|
|
|
* @param {number} i The index to read. |
|
91
|
|
|
* @return {number} |
|
92
|
|
|
*/ |
|
93
|
|
|
"read32BitFloat": function (bytes, i) { |
|
94
|
|
|
return intBits.pack(BitReader["read32Bit"](bytes, i)); |
|
95
|
|
|
}, |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Read 1 40-bit int from from bytes. |
|
99
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
100
|
|
|
* @param {number} i The index to read. |
|
101
|
|
|
* @return {number} |
|
102
|
|
|
*/ |
|
103
|
|
|
"read40Bit": function (bytes, i) { |
|
104
|
|
|
return readBytesAsBits(bytes, i, 5); |
|
105
|
|
|
}, |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Read 1 48-bit int from bytes. |
|
109
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
110
|
|
|
* @param {number} i The index to read. |
|
111
|
|
|
* @return {number} |
|
112
|
|
|
*/ |
|
113
|
|
|
"read48Bit": function (bytes, i) { |
|
114
|
|
|
return readBytesAsBits(bytes, i, 6); |
|
115
|
|
|
}, |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Read 1 64-bit double from bytes. |
|
119
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
120
|
|
|
* @param {number} i The index to read. |
|
121
|
|
|
* @return {number} |
|
122
|
|
|
*/ |
|
123
|
|
|
"read64BitFloat": function (bytes, i) { |
|
124
|
|
|
return floats.decodeFloat64(bytes.slice(i,i+8)); |
|
125
|
|
|
}, |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Read 1 char from bytes. |
|
129
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
130
|
|
|
* @param {number} i The index to read. |
|
131
|
|
|
* @return {string} |
|
132
|
|
|
*/ |
|
133
|
|
|
"readChar": function (bytes, i, type) { |
|
134
|
|
|
let chrs = ""; |
|
135
|
|
|
let j = 0; |
|
136
|
|
|
let len = type.bits / 8; |
|
137
|
|
|
while(j < len) { |
|
138
|
|
|
chrs += String.fromCharCode(bytes[i+j]); |
|
139
|
|
|
j++; |
|
140
|
|
|
} |
|
141
|
|
|
return chrs; |
|
142
|
|
|
} |
|
143
|
|
|
}; |
|
144
|
|
|
|
|
145
|
|
|
module.exports = BitReader; |
|
146
|
|
|
|